﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace COMP376A3_9609695
{
    class Block : Drawable, Updates
    {
        private Vector3 position;
        private Model blockModel;
        private Matrix blockModelMat;
        public float X { get { return position.X; } set { position.X = value; } }
        public float Y { get { return position.Y; } set { position.Y = value; } }
        public float Z { get { return position.Z; } set { position.Z = value; } }
        private float fallSpeed;
        public bool colliding;

        public bool sparked { get; private set; }
        public Sparks sparksL, sparksR;

        public Block(Model model, float X, float Y, float Z)
        {
            position = new Vector3(X, Y, Z);
            blockModel = model;
            blockModelMat = Matrix.CreateTranslation(-1* model.Meshes.First().BoundingSphere.Center);
            blockModelMat *= Matrix.CreateTranslation(position);
            fallSpeed = 20;
            colliding = false;
            sparked = false;
        }

        public void Update(GameTime time)
        {
            int elapsed = time.ElapsedGameTime.Milliseconds + 1;
            float dY = fallSpeed / elapsed;

            if (Y > 0)
                Y -= dY;
            if (Y <= 0)
            {
                Y = 0;
                if (!sparked)
                {
                    sparksL = new Sparks(X + 1, Y - 1, Z - 1, 50);
                    sparksR = new Sparks(X - 1, Y - 1, Z - 1, 50);
                    sparked = true;
                }
            }
            if (sparksL != null)
            {
                sparksL.Update(time);
                sparksR.Update(time);
            }

            blockModelMat = Matrix.CreateTranslation(-1 * blockModel.Meshes.First().BoundingSphere.Center);
            blockModelMat = Matrix.CreateTranslation(0, -0.5f, 0);
            blockModelMat *= Matrix.CreateTranslation(position);
                Vector3 color;
                if (sparksL != null)
                    color = new Vector3(0.9f, 0.9f, 0.9f);
                else
                    color = new Vector3(0.4f, 0.4f, 0.4f);
            if (colliding)
            {
                color = new Vector3(0.8f, 0.4f, 0.0f);
            }
            foreach (BasicEffect effect in blockModel.Meshes.First().Effects)
            {
                effect.DiffuseColor = color;
            }

        }

        public Model getModel()
        {
            return blockModel;
        }

        public Matrix getModelMatrix()
        {
            return blockModelMat;
        }
    }
}
